Rsync over SSH

Here is a simple backup solution for a client to remote server. Assuming you've already set up a Hardened SSH Server with key login

In this example I'm backing up my Nextcloud data (pictures, documents, etc.) to a remote server for my 'offsite backup'

#!/bin/bash

# Variables
TARGET="nextcloud"
SOURCE_DIR="/mnt/LOCALDRIVE/${TARGET}"
DEST_USER="REMOTEUSER"
DEST_HOST="REMOTESERVER.lan"
## directory is inclusive, it will also copy folder along with it
DEST_DIR="/mnt/REMOTESTORAGE"

LOG_DIR="/home/LOCALUSER/scripts/backups/${TARGET}"
NOW=`date +'%Y-%m-%d_%H%M%S'`
LOG_FILE="${LOG_DIR}/sync.log" 
STATUS_FILE="${LOG_DIR}/status.log"

echo "$NOW \n" > "$LOG_FILE"

# Rsync command
rsync -avzP -e ssh "$SOURCE_DIR" "${DEST_USER}@${DEST_HOST}:${DEST_DIR}" >> "$LOG_FILE" 2>&1

RSYNC_EXIT_CODE=$?

LINECOUNT=$(wc -l sync.log)

# Update status based on exit code
if [ $RSYNC_EXIT_CODE -eq 0 ]; then
    echo "OK $NOW $LINECOUNT" > "$STATUS_FILE"
else
    echo "ERROR $NOW $LINECOUNT" > "$STATUS_FILE"
fi

Troubleshooting

I'm getting 2 different errors when trying to sync my nextcloud data files

  1. could not make way for new regular file: /FILEPATH
  2. cannot delete non-empty directory: /FILEPATH